package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;


import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.ImageEditor;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ReduceLog;
import reduce.fz.com.reduce.util.SDCardImageLoader;
import reduce.fz.com.reduce.util.ScreenUtils;

/**
 * 上传编辑页面的图片适配器
 * 这个是给gridView用的
 */
public class ImageEditorPreviewAdapter extends BaseAdapter {
    private Context mContext;
    /**
     * 图片列表
     */
    private ArrayList<String> mImagePathList = new ArrayList<String>();
    private ArrayList<String> mHoldImagePathList = new ArrayList<String>();
    /**
     * sd卡图片加载器
     */
    private SDCardImageLoader loader;
    private OnAddImageListener mOnAddImageListener;
    /**
     * 图片展示大小根据屏幕宽度而定
     */
    private final int imageWidth ;
    /**
     * 发布最多卡片数
     */
    private final int MAX_IMAGE_COUNT;

    /**
     * 添加类型
     */
    private final static int TYPE_ADD = 1;
    /**
     * 编辑类型
     */
    private final static int TYPE_EDITOR = 0;
    /**
     * hold 类型
     */
    private final static int TYPE_HOLD = 2;


    public ImageEditorPreviewAdapter(Context context, List<ImageEditor> imageEditorList, OnAddImageListener onAddImageListener){
        this.mContext = context;
        if( imageEditorList == null ){
            imageEditorList = new ArrayList<ImageEditor>();
        }
        MAX_IMAGE_COUNT = context.getResources().getInteger(R.integer.card_publish_max_image_count);
        while( imageEditorList.size() > getMaxImageCount()){
            imageEditorList.remove(imageEditorList.size()-1);
        }

        this.mOnAddImageListener = onAddImageListener;
        for(ImageEditor imageEditor : imageEditorList){
            if(imageEditor!=null){
                if(TextUtils.isEmpty(imageEditor.newPath)){
                    this.mImagePathList.add(imageEditor.originalPath);
                } else {
                    this.mImagePathList.add(imageEditor.newPath);
                }
            }
        }
        loader = HttpUtils.getSDCardImageLoader();
        //屏幕宽度+
        int sw = ScreenUtils.getScreenW();
        imageWidth = (sw-context.getResources().getDimensionPixelSize(R.dimen.dimen_36)*5)/4;
    }

    /**
     * 替换hold image
     * @param holdImageList
     */
    public void replaceHoldImage(List<String> holdImageList){
        mHoldImagePathList.clear();
        if( holdImageList != null ) {
            mHoldImagePathList.addAll(holdImageList);
        }
        notifyDataSetChanged();
    }

    /**
     * 清空原来的，换成现在的
     * @param imageEditorList
     */
    public void replaceImage(List<ImageEditor> imageEditorList){
        mImagePathList.clear();
        if(imageEditorList!=null){
            for(ImageEditor imageEditor : imageEditorList){
                if(imageEditor!=null){
                    if(TextUtils.isEmpty(imageEditor.newPath)){
                        this.mImagePathList.add(imageEditor.originalPath);
                    } else {
                        this.mImagePathList.add(imageEditor.newPath);
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        int imageSize = mImagePathList.size();
        if( position < imageSize ){
            return mImagePathList.get(position);
        } else if ( position == imageSize ){
            return null;
        } else {
            return mHoldImagePathList.get( position - 1 - imageSize );
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final String filePath = (String) getItem(position);
        final int viewType = getItemViewType(position);

        if( viewType == TYPE_ADD ){//增加按钮
            if ( convertView == null ) {
                convertView = LayoutInflater.from( mContext ).inflate(R.layout.view_image_editor, null);
                AddViewHolder holder = new AddViewHolder( convertView , imageWidth  , this );
                convertView.setTag(holder);
            }
        } else if( viewType == TYPE_HOLD ){//hold 图片的
            final HoldImageViewHolder holder;
            if ( convertView == null ) {
                convertView = LayoutInflater.from( mContext ).inflate(R.layout.view_image_editor, null);
                holder = new HoldImageViewHolder( convertView , imageWidth , this );
                convertView.setTag(holder);
            }else {
                holder = (HoldImageViewHolder) convertView.getTag();
            }
            holder.renderView(filePath, position);
        } else { //正常的
            final EditorViewHolder holder;
            if ( convertView == null ) {
                convertView = LayoutInflater.from( mContext ).inflate(R.layout.view_image_editor, null);
                holder = new EditorViewHolder( convertView , imageWidth , this );
                convertView.setTag(holder);
            }else {
                holder = (EditorViewHolder) convertView.getTag();
            }
            holder.renderView(filePath,position);
        }
        return convertView;
    }
    @Override
    public int getViewTypeCount() {
        return 3;
    }
    @Override
    public int getItemViewType(int position) {
        int imageSize = mImagePathList.size();
        if( position < imageSize ){
            return TYPE_EDITOR;
        } else if ( position == imageSize && position < getMaxImageCount() ){
            return TYPE_ADD;
        } else {
            return TYPE_HOLD;
        }

    }

    @Override
    public int getCount() {
        int num = mImagePathList.size() ;
        if( num < getMaxImageCount() ){
            num++;
        }
        num += mHoldImagePathList.size();
        return num;
    }

    private int getMaxImageCount(){
        return MAX_IMAGE_COUNT - mHoldImagePathList.size();
    }

    public static class ImageViewHolder {
        public ImageEditorPreviewAdapter mAdapter;
        public ImageButton mDeleteButton;
        public ImageView mImageView;
        public int mIndex;

        public ImageViewHolder(View view, int imageWidth , ImageEditorPreviewAdapter adapter) {
            mAdapter = adapter;
            mImageView = (ImageView) view.findViewById(R.id.image_editor_thumb);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(imageWidth , imageWidth);
            mImageView.setLayoutParams(layoutParams);
            mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mDeleteButton = (ImageButton) view.findViewById(R.id.image_editor_del_btn);
        }

        public void renderView( String filePath , int positon ){
            mIndex = positon;
            mAdapter.loader.loadImage( 4  , filePath , mImageView );
        }

    }

    /**
     * EditorViewHolder
     */
    public static class EditorViewHolder extends ImageViewHolder implements View.OnClickListener{
        public EditorViewHolder( View v, int imageWidth , ImageEditorPreviewAdapter adapter) {
            super(v,imageWidth,adapter);
            mDeleteButton.setOnClickListener(this);
            mImageView.setOnClickListener(this);
        }
        @Override
        public void renderView(String filePath , int positon){
            super.renderView(filePath, positon);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.image_editor_del_btn:
                    mAdapter.mImagePathList.remove(mIndex);
                    mAdapter.notifyDataSetChanged();
                    if( mAdapter.mOnAddImageListener != null ){
                        try{
                            mAdapter.mOnAddImageListener.onRemoveImage( mIndex );
                        }catch(Exception e){
                            ReduceLog.e(e);
                        }
                    }
                    break;
                case R.id.image_editor_thumb:
                    if( mAdapter.mOnAddImageListener != null ){
                        try{
                            mAdapter.mOnAddImageListener.onEditorClick( mIndex );
                        }catch(Exception e){
                            ReduceLog.e(e);
                        }
                    }
                    break;
            }
        }
    }

    /**
     * AddViewHolder
     */
    public static class AddViewHolder extends ImageViewHolder implements View.OnClickListener{

        public AddViewHolder( View v , int imageWidth , ImageEditorPreviewAdapter adapter ) {
            super(v, imageWidth, adapter);
            mImageView.setBackgroundResource(R.drawable.push_photo_bg);
            mImageView.setScaleType(ImageView.ScaleType.CENTER);
            mImageView.setImageResource(R.drawable.push_icon_photo);
            mDeleteButton.setVisibility(View.GONE);
            mImageView.setOnClickListener(this);
        }
        @Override
        public void renderView(String filePath , int positon){
        }
        @Override
        public void onClick(View view) {
            if( mAdapter.mOnAddImageListener != null ){
                try{
                    mAdapter.mOnAddImageListener.needSelectImage( mAdapter.getMaxImageCount() , mAdapter.getMaxImageCount() - mAdapter.mImagePathList.size() );
                }catch(Exception e){
                    ReduceLog.e(e);
                }
            }
        }
    }

    /**
     * HoldImageViewHolder
     */
    public static class HoldImageViewHolder extends ImageViewHolder{
        public HoldImageViewHolder( View v , int imageWidth  , ImageEditorPreviewAdapter adapter) {
            super(v, imageWidth, adapter);
            mDeleteButton.setVisibility(View.GONE);
        }
    }

    /**
     * 需要继续添加图片
     */
    public interface OnAddImageListener {
        /**
         * 需要继续添加
         * @param needCount
         */
        void needSelectImage(int maxCount, int needCount);

        /**
         * 点击编辑按钮
         * @param position
         */
        void onEditorClick(int position);

        /**
         * 移除某个图片
         * @param position
         */
        void onRemoveImage(int position);
    }
}
